package cn.ivicar.oss.controller;

import cn.com.carsmart.exception.CarsmartException;
import cn.com.carsmart.ws.ibatis.model.BaseExample;
import cn.com.carsmart.ws.model.*;
import cn.com.carsmart.ws.service.*;
import cn.ivicar.oss.entity.ResponseEntity;
import cn.ivicar.oss.util.JsonUtils;
import cn.ivicar.sim.controller.SimController;
import com.google.inject.Inject;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author: Administrator
 * @Description:SIM卡信息控制层
 * @Date: Created in 2017/5/16
 */
public class SimInfoController {

    @Inject
    private SimInfoManager simInfoManager;
    @Inject
    private SimFlowInfoManager simFlowInfoManager;
    @Inject
    private SimFlowDailyStatManager simFlowDailyStatManager;
    @Inject
    private SimFlowMonthlyStatManager simFlowMonthlyStatManager;
    @Inject
    private SimController simController;
    @Inject
    private SimChargeRecordManager simChargeRecordManager;
    @Inject
    private SimFlowPackageInfoManager simFlowPackageInfoManager;

    @Inject
    private VehicleSimManager vehicleSimManager;
    @Inject
    private VehicleManager vehicleManager;

    /*
    通过iccid获取SIM卡信息
     */
    public JSONObject getSimInfoByIccid(String iccid) throws CarsmartException {
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isNotBlank(iccid)) {
            BaseExample example = this.simInfoManager.getExample();
            example.or().andPropertyEqualTo(iccid, "iccid");
            List<SimInfo> simInfos = this.simInfoManager.listByBaseExample(example);
            if (simInfos != null && simInfos.size() > 0) {
                jsonObject = JsonUtils.getSimInfoJSON(simInfos.get(0));

                jsonObject.put("combo", "包月");//套餐类型
                jsonObject.put("flowSum", 0);//流量总量
                jsonObject.put("flowMoney", 0);//流量花费

                BaseExample example1 = this.simFlowInfoManager.getExample();
                example1.or().andPropertyEqualTo(simInfos.get(0).getIccid(), "iccid");
                List<SimFlowInfo> simFlowInfos =
                    this.simFlowInfoManager.listByBaseExample(example1);
                if (simFlowInfos != null && simFlowInfos.size() > 0) {
                    SimFlowInfo simFlowInfo = simFlowInfos.get(simFlowInfos.size() - 1);
                    jsonObject.put("combo", simFlowInfo.getFlowPack());//套餐类型
                    jsonObject.put("flowSum", simFlowInfo.getFlowAmount());//流量总量
                    jsonObject.put("flowMoney", simFlowInfo.getFlowUsed());//流量花费
                }

                return jsonObject;
            }
        }

        return jsonObject;
    }

    //分页显示卡列表
    public JSONArray getSimInfoList() throws CarsmartException {
        JSONArray jsonArray = new JSONArray();
        List<SimInfo> simInfos = this.simInfoManager.list();
        if (simInfos != null && simInfos.size() > 0) {
            for (SimInfo s : simInfos) {
                JSONObject jsonObject = JsonUtils.getSimInfoJSON(s);
                jsonObject.put("combo", "包月");//套餐类型
                jsonObject.put("flowSum", 0);//流量总量
                jsonObject.put("flowMoney", 0);//流量花费

                BaseExample example1 = this.simFlowInfoManager.getExample();
                example1.or().andPropertyEqualTo(s.getIccid(), "iccid");
                List<SimFlowInfo> simFlowInfos =
                    this.simFlowInfoManager.listByBaseExample(example1);
                if (simFlowInfos != null && simFlowInfos.size() > 0) {
                    SimFlowInfo simFlowInfo = simFlowInfos.get(simFlowInfos.size() - 1);
                    jsonObject.put("combo", simFlowInfo.getFlowPack());//套餐类型
                    jsonObject.put("flowSum", simFlowInfo.getFlowAmount());//流量总量
                    jsonObject.put("flowMoney", simFlowInfo.getFlowUsed());//流量花费
                }

                jsonArray.add(jsonObject);
            }
        }

        return jsonArray;
    }

    /*
    通过iccid获取设备ID
     */
    public Long getVehicleIdByIccid(String iccid) throws CarsmartException {
        BaseExample example = this.vehicleSimManager.getExample();
        example.or().andPropertyEqualTo(iccid, "iccid").andPropertyEqualTo(1, "status");
        List<VehicleSim> vehicleSims = this.vehicleSimManager.listByBaseExample(example);
        if (vehicleSims != null && vehicleSims.size() > 0) {
            return vehicleSims.get(0).getVehicleId();
        }

        return null;
    }

    /*
    通过iccid获取设备信息
     */
    public JSONArray getVehicles(String iccid) throws CarsmartException {
        if (StringUtils.isBlank(iccid)) {
            return null;
        }
        JSONArray jsonArray = new JSONArray();
        BaseExample example = this.vehicleSimManager.getExample();
        example.or().andPropertyEqualTo(iccid, "iccid").andPropertyEqualTo(1, "status");
        ;
        List<VehicleSim> vehicleSims = this.vehicleSimManager.listByBaseExample(example);
        if (vehicleSims != null && vehicleSims.size() > 0) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (VehicleSim vs : vehicleSims) {
                JSONObject vehicleObj = getVehicleById(vs.getVehicleId());
                if (vehicleObj != null) {
                    //增加绑定时间
                    Date create = vs.getCreated();
                    String createStr = sdf.format(create);
                    vehicleObj.put("bindTime", createStr);

                    jsonArray.add(vehicleObj);
                }
            }
        }

        return jsonArray;
    }

    /*
    通过设备ID获取设备信息
     */
    public JSONObject getVehicleById(Long id) throws CarsmartException {

        BaseExample example = this.vehicleManager.getExample();
        example.or().andPropertyEqualTo(id, "id");
        List<Vehicle> vehicles = this.vehicleManager.listByBaseExample(example);
        if (vehicles != null && vehicles.size() > 0) {
            JSONObject vehicleJSON = JsonUtils.getVehicleJSON(vehicles.get(0));
            return vehicleJSON;
        }

        return null;
    }

    /*
    通过iccid和设备id删除设备信息
     */
    public ResponseEntity deleteVehicle(String iccid,
        String vid) throws CarsmartException {

        ResponseEntity responseEntity = new ResponseEntity();
        //缺少iccid
        if (StringUtils.isBlank(iccid)) {
            responseEntity.setCode(400);
            responseEntity.setMessage("删除失败！未获取sim卡标识!");
            return responseEntity;
        }
        //缺少设备ID
        if (StringUtils.isBlank(vid)) {
            responseEntity.setCode(400);
            responseEntity.setMessage("删除失败！未获取车辆标识!");
            return responseEntity;
        }

        BaseExample example = this.vehicleSimManager.getExample();
        example.or().andPropertyEqualTo(Long.parseLong(vid), "vehicleId")
            .andPropertyEqualTo(iccid, "iccid").andPropertyEqualTo(1, "status");
        List<VehicleSim> vehicleSims = this.vehicleSimManager.listByBaseExample(example);
        if (vehicleSims != null && vehicleSims.size() > 0) {
            VehicleSim vehicleSim = vehicleSims.get(0);
            vehicleSim.setStatus(0);
            this.vehicleSimManager.updateSelective(vehicleSim);
            return responseEntity;//删除成功
        } else {
            responseEntity.setCode(400);
            responseEntity.setMessage("删除失败！关系异常，未查询到关联关系!");//没有关联关系，删除失败
            return responseEntity;
        }
    }

    /*
    新增设备和SIM卡关系
     */
    public ResponseEntity addVehicleRel(JSONObject jsonObject) throws CarsmartException {
        ResponseEntity responseEntity = new ResponseEntity();
        if (null == jsonObject) {
            responseEntity.setCode(400);
            responseEntity.setMessage("参数异常!对象为空！");
            return responseEntity;
        }
        String iccid = jsonObject.getString("iccid");
        if (StringUtils.isBlank(iccid)) {
            responseEntity.setCode(400);
            responseEntity.setMessage("未获取sim卡标识!");
            return responseEntity;
        }
        String value = jsonObject.getString("value");
        if (StringUtils.isBlank(value)) {
            responseEntity.setCode(400);
            responseEntity.setMessage("车辆信息不能为空!");
            return responseEntity;
        }
        String type = jsonObject.optString("type", "2");
        BaseExample example = this.vehicleManager.getExample();
        BaseExample.Criteria or = example.or();
        if ("1".equalsIgnoreCase(type)) {
            //大驾号
            or.andPropertyEqualTo(value, "serial");
        } else {
            //车牌号
            or.andPropertyEqualTo(value, "licensePlate");
        }
        List<Vehicle> vehicles = this.vehicleManager.listByBaseExample(example);
        if (vehicles != null && vehicles.size() > 0) {
            Long id = vehicles.get(0).getId();

            BaseExample example1 = this.vehicleSimManager.getExample();
            example1.or().andPropertyEqualTo(id, "vehicleId").andPropertyEqualTo(iccid, "iccid");
            List<VehicleSim> vehicleSims = this.vehicleSimManager.listByBaseExample(example1);
            if (vehicleSims != null && vehicleSims.size() > 0) {
                //表中已经存在vid和iccid的已删除状态的绑定关系，防止插入失败，更新状态
                VehicleSim vehicleSim = vehicleSims.get(0);
                vehicleSim.setStatus(1);
                vehicleSim.setCreated(new Date());
                vehicleSim.setUpdated(new Date());
                this.vehicleSimManager.updateSelective(vehicleSim);
                return responseEntity;
            } else {
                VehicleSim vehicleSim = new VehicleSim();
                vehicleSim.setIccid(iccid);
                vehicleSim.setVehicleId(id);
                vehicleSim.setStatus(1);
                vehicleSim.setCreated(new Date());
                vehicleSim.setUpdated(new Date());
                try {
                    this.vehicleSimManager.insert(vehicleSim);
                    return responseEntity;
                } catch (Exception e) {
                    responseEntity.setCode(400);
                    responseEntity.setMessage("关系已存在，重复添加!");
                    return responseEntity;
                }
            }

        } else {
            responseEntity.setCode(400);
            responseEntity.setMessage("车辆不存在!");
            return responseEntity;
        }
    }

    /*
    获取设备状态
     */
    public JSONArray getDeviceStatus() throws CarsmartException {

        JSONArray jsonArray = new JSONArray();
        //
        BaseExample example = this.simInfoManager.getExample();
        JSONObject jsonObject = new JSONObject();

        example.or().andPropertyEqualTo(0, "switchStatus");//关机
        int i = this.simInfoManager.countByExample(example);
        jsonObject.put("name", "关机");
        jsonObject.put("value", i);
        jsonArray.add(jsonObject);

        example.clear();
        example.or().andPropertyEqualTo(1, "switchStatus");//开机
        int j = this.simInfoManager.countByExample(example);
        jsonObject.put("name", "开机");
        jsonObject.put("value", j);
        jsonArray.add(jsonObject);

        example.clear();
        example.or().andPropertyEqualTo(2, "switchStatus");//未知
        int k = this.simInfoManager.countByExample(example);
        jsonObject.put("name", "未知");
        jsonObject.put("value", k);
        jsonArray.add(jsonObject);

        return jsonArray;

    }

    /*
    获取用户相关信息
     */
    public JSONArray getUserStatus() throws CarsmartException {

        JSONArray jsonArray = new JSONArray();

        BaseExample example = this.simInfoManager.getExample();
        JSONObject jsonObject = new JSONObject();
        //（00：正常  01：未知  02：停机 03：待激活 04：销号）
        example.or().andPropertyEqualTo("00", "carStatus");//正常
        int i = this.simInfoManager.countByExample(example);
        jsonObject.put("name", "正常");
        jsonObject.put("value", i);
        jsonArray.add(jsonObject);

        example.clear();
        example.or().andPropertyEqualTo("01", "carStatus");//未知
        int j = this.simInfoManager.countByExample(example);
        jsonObject.put("name", "未知");
        jsonObject.put("value", j);
        jsonArray.add(jsonObject);

        example.clear();
        example.or().andPropertyEqualTo("02", "carStatus");//停机
        int k = this.simInfoManager.countByExample(example);
        jsonObject.put("name", "停机");
        jsonObject.put("value", k);
        jsonArray.add(jsonObject);

        example.clear();
        example.or().andPropertyEqualTo("03", "carStatus");//待激活
        int m = this.simInfoManager.countByExample(example);
        jsonObject.put("name", "待激活");
        jsonObject.put("value", m);
        jsonArray.add(jsonObject);


        example.clear();
        example.or().andPropertyEqualTo("04", "carStatus");//销号
        int n = this.simInfoManager.countByExample(example);
        jsonObject.put("name", "销号");
        jsonObject.put("value", n);
        jsonArray.add(jsonObject);

        return jsonArray;
    }

    public JSONObject getSevenFlow() throws CarsmartException {

        JSONObject jsonObject = new JSONObject();

        String[] td = new String[7];

        int[] vd = new int[7];

        BaseExample example = this.simFlowDailyStatManager.getExample();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Calendar c = Calendar.getInstance();
        Date date = new Date();
        for (int i = 0; i < 7; i++) {
            c.setTime(date);
            c.add(Calendar.DATE, -1);
            c.set(Calendar.HOUR_OF_DAY, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            date = c.getTime();
            String format = sdf.format(date);
            td[i] = format;

            example.clear();
            example.or().andPropertyEqualTo(Integer.parseInt(format), "occurDate");
            List<SimFlowDailyStat> simFlowDailyStats =
                this.simFlowDailyStatManager.listByBaseExample(example);
            long dailyUsed = 0;
            for (SimFlowDailyStat sfds : simFlowDailyStats) {
                dailyUsed += sfds.getFlowUsed();
            }
            vd[i] = (int) dailyUsed;

        }

        jsonObject.put("timeData", td);
        jsonObject.put("valueData", vd);

        return jsonObject;
    }

    /*
    获取月流量排行
     */
    public JSONArray getMongthRank() throws CarsmartException {

        JSONArray jsonArray = new JSONArray();
        //
        JSONObject jsonObject = new JSONObject();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        String format = sdf.format(new Date());
        BaseExample example = this.simFlowMonthlyStatManager.getExample();
        example.or().andPropertyEqualTo(Integer.parseInt(format), "occurMonth");
        example.setOrderByClause(" flowUsed desc ");//降序排列
        example.setLimitCount(5);

        List<SimFlowMonthlyStat> simFlowMonthlyStats =
            this.simFlowMonthlyStatManager.listByBaseExample(example);
        if (simFlowMonthlyStats != null && simFlowMonthlyStats.size() > 0) {
            for (SimFlowMonthlyStat sfms : simFlowMonthlyStats) {
                jsonObject.put("iccid", sfms.getIccid());
                jsonObject.put("value", sfms.getFlowUsed());
                jsonArray.add(jsonObject);
            }
        }

        return jsonArray;
    }

    public JSONArray getNewSimList() throws CarsmartException {

        JSONArray jsonArray = new JSONArray();
        //
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userCode", "1362546988");//卡号 ----
        jsonObject.put("iccid", "898602B5191690241164");//iccid ---
        jsonObject.put("operator", "移动");//运营商+++++
        jsonObject.put("carStatus", "开机");//设备状态---------
        jsonObject.put("smsStatus", "开通"); //短信 ---
        jsonObject.put("flowOnline", 1);//流量在线状态  1在线 0 离线
        jsonObject.put("GPRS", 1);//GPRS状态  （00：离线  01：在线  02：未知）

        jsonObject.put("userAff", "鑫洋泉电子科技有限公司");//归属客户
        jsonObject.put("combo", "包月");//套餐类型
        jsonObject.put("flowSum", 1024);//流量总量
        jsonObject.put("flowMoney", 20);//流量花费

        jsonArray.add(jsonObject);

        jsonObject.put("userCode", "1362546989");
        jsonObject.put("iccid", "898602B5191690241165");
        jsonObject.put("deviceStatus", 0);//设备状态 1开机 0 关机
        jsonObject.put("flowOnline", 0);//流量在线状态  1在线 0 离线
        jsonObject.put("GPRS", 1);//GPRS状态  1正常 0 不正常
        jsonObject.put("userStatus", 1);//用户状态
        jsonObject.put("userAff", "鑫洋泉电子科技有限公司");//归属客户
        jsonObject.put("combo", "包月");//套餐类型
        jsonObject.put("flowSum", 1024);//流量总量
        jsonObject.put("flowMoney", 20);//流量花费
        jsonArray.add(jsonObject);


        return jsonArray;
    }

    //根据iccid获取SIM卡消费账单
    public JSONArray getSimConsumeList(String iccid) throws CarsmartException {

        JSONArray jsonArray = new JSONArray();
        if (StringUtils.isBlank(iccid)) {
            return jsonArray;
        }
        //
        BaseExample example = this.simFlowDailyStatManager.getExample();
        example.or().andPropertyEqualTo(iccid, "iccid");
        example.setOrderByClause("occurDate desc ");
        List<SimFlowDailyStat> simFlowDailyStats =
            this.simFlowDailyStatManager.listByBaseExample(example);
        JSONObject jsonObject = new JSONObject();
        if (simFlowDailyStats != null && simFlowDailyStats.size() > 0) {
            for (SimFlowDailyStat sdfs : simFlowDailyStats) {
                jsonObject.put("iccid", iccid);//iccid
                jsonObject.put("flowConsume", sdfs.getFlowUsed());//流量消耗
                jsonObject.put("statDate", sdfs.getOccurDate());//统计日期

                jsonArray.add(jsonObject);
            }
        }

        return jsonArray;
    }

    /*
    根据iccid获取流量日统计信息
     */
    public JSONObject getSimDailyStat(String iccid) throws CarsmartException {

        //
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isBlank(iccid)) {
            return jsonObject;
        }

        String[] td = new String[30];

        int[] vd = new int[30];

        BaseExample example = this.simFlowDailyStatManager.getExample();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Calendar c = Calendar.getInstance();
        Date date = new Date();
        for (int i = 0; i < 30; i++) {
            c.setTime(date);
            c.add(Calendar.DATE, -1);
            c.set(Calendar.HOUR_OF_DAY, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            date = c.getTime();
            String format = sdf.format(date);
            td[i] = format;

            example.clear();
            example.or().andPropertyEqualTo(Integer.parseInt(format), "occurDate")
                .andPropertyEqualTo(iccid, "iccid");
            List<SimFlowDailyStat> simFlowDailyStats =
                this.simFlowDailyStatManager.listByBaseExample(example);
            if (simFlowDailyStats != null && simFlowDailyStats.size() > 0) {
                SimFlowDailyStat simFlowDailyStat = simFlowDailyStats.get(0);
                vd[i] = (int) simFlowDailyStat.getFlowUsed().longValue();
            } else {
                vd[i] = 0;
            }
        }

        jsonObject.put("timeData", td);
        jsonObject.put("valueData", vd);

        return jsonObject;
    }

    /*
    根据iccid获取流量月统计信息
     */
    public JSONObject getSimMonthyStat(String iccid) throws CarsmartException {

        //
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isBlank(iccid)) {
            return jsonObject;
        }

        String[] td = new String[12];
        int[] vd = new int[12];


        BaseExample example = this.simFlowMonthlyStatManager.getExample();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        Calendar c = Calendar.getInstance();
        Date date = new Date();
        for (int i = 0; i < 12; i++) {
            c.setTime(date);
            c.add(Calendar.MONTH, -1);
            c.set(Calendar.HOUR_OF_DAY, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            date = c.getTime();
            String format = sdf.format(date);
            td[i] = format;

            example.clear();
            example.or().andPropertyEqualTo(Integer.parseInt(format), "occurMonth")
                .andPropertyEqualTo(iccid, "iccid");
            List<SimFlowMonthlyStat> simFlowMonthlyStats =
                this.simFlowMonthlyStatManager.listByBaseExample(example);
            if (simFlowMonthlyStats != null && simFlowMonthlyStats.size() > 0) {
                SimFlowMonthlyStat sfms = simFlowMonthlyStats.get(0);
                vd[i] = (int) sfms.getFlowUsed().longValue();
            } else {
                vd[i] = 0;
            }
        }
        jsonObject.put("timeData", td);
        jsonObject.put("valueData", vd);

        return jsonObject;
    }

    /*
    新增SIM卡信息
     */
    public ResponseEntity addSimInfo(JSONObject jsonObject) throws CarsmartException {

        ResponseEntity responseEntity = new ResponseEntity();
        if (null == jsonObject) {
            responseEntity.setCode(400);
            responseEntity.setMessage("参数异常!对象为空！");
            return responseEntity;
        }
        if (StringUtils.isBlank(jsonObject.optString("iccid"))) {
            responseEntity.setCode(400);
            responseEntity.setMessage("iccid不能为空！");
            return responseEntity;
        }

        SimInfo simInfo = simController.getSimInfo(jsonObject.optString("iccid"));
        if (simInfo == null) {
            //失败返回：
            responseEntity.setCode(400);
            responseEntity.setMessage("添加sim卡信息失败！");
            return responseEntity;
        }

        return responseEntity;
    }

    /*
    更新gprs状态（00：离线  01：在线  02：未知）
     */
    public ResponseEntity changeGPRS(JSONObject jsonObject) throws CarsmartException {

        ResponseEntity responseEntity = new ResponseEntity();
        if (null == jsonObject) {
            responseEntity.setCode(400);
            responseEntity.setMessage("参数异常!对象为空！");
            return responseEntity;
        }
        if (StringUtils.isBlank(jsonObject.optString("iccid"))) {
            responseEntity.setCode(400);
            responseEntity.setMessage("iccid不能为空！");
            return responseEntity;
        }

        SimInfo simInfo = simController.getSimInfo(jsonObject.optString("iccid"));
        if (simInfo == null) {
            responseEntity.setCode(400);
            responseEntity.setMessage("iccid对应的sim卡信息为空！");
            return responseEntity;
        }

        String targetStatus = "00".equals(simInfo.getIsPauseGprs()) ? "01" : "00";
        try {
            simController.updateGPRSStatus(jsonObject.optString("iccid"), targetStatus);
        } catch (Exception e) {
            responseEntity.setCode(400);
            responseEntity.setMessage(e.getMessage());
            return responseEntity;
        }

        return responseEntity;
    }


    /*
    更新卡状态信息
    卡状态（00：正常  01：未知  02：停机 03：待激活 04：销号）
     */
    public ResponseEntity changeCarStatus(JSONObject jsonObject) throws CarsmartException {

        ResponseEntity responseEntity = new ResponseEntity();
        if (null == jsonObject) {
            responseEntity.setCode(400);
            responseEntity.setMessage("参数异常!对象为空！");
            return responseEntity;
        }
        if (StringUtils.isBlank(jsonObject.optString("iccid"))) {
            responseEntity.setCode(400);
            responseEntity.setMessage("iccid不能为空！");
            return responseEntity;
        }

        SimInfo simInfo = simController.getSimInfo(jsonObject.optString("iccid"));
        if (simInfo == null) {
            responseEntity.setCode(400);
            responseEntity.setMessage("iccid对应的sim卡信息为空！");
            return responseEntity;
        }

        String targetStatus = "00".equals(simInfo.getIsPauseGprs()) ? "01" : "00";
        try {
            simController.updateSwitchStatus(jsonObject.optString("iccid"), targetStatus);
        } catch (Exception e) {
            responseEntity.setCode(400);
            responseEntity.setMessage(e.getMessage());
            return responseEntity;
        }

        return responseEntity;
    }

    /*
    根据iccid获取充值记录
     */
    public JSONArray chargeRecord(String iccid) throws CarsmartException {
        JSONArray jsonArray = new JSONArray();
        if (StringUtils.isBlank(iccid)) {
            return jsonArray;
        }
        BaseExample example = this.simChargeRecordManager.getExample();
        example.or().andPropertyEqualTo(iccid, "iccid");
        example.setOrderByClause("created desc ");
        List<SimChargeRecord> simChargeRecords =
            this.simChargeRecordManager.listByBaseExample(example);
        JSONObject jsonObject = new JSONObject();
        if (simChargeRecords != null && simChargeRecords.size() > 0) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SimChargeRecord scr : simChargeRecords) {
                jsonObject.put("iccid", iccid);//iccid
                jsonObject.put("amount", scr.getChargeAmount());//充值金额
                jsonObject.put("packageId", scr.getFlowPackageId());//套餐id
                String time = sdf.format(scr.getCreated());
                jsonObject.put("time", time);//充值时间

                String uptime = sdf.format(scr.getModified());
                jsonObject.put("uptime", uptime);//修改时间

                String status = "未知";
                if (scr.getChargeStatus() != null) {
                    switch (scr.getChargeStatus()) {
                        case 1:
                            status = "未支付";
                            break;
                        case 4:
                            status = "充值成功";
                            break;
                        case 3:
                            status = "充值失败";
                            break;
                    }
                }
                jsonObject.put("status", status);//充值状态

                jsonArray.add(jsonObject);
            }
        }

        return jsonArray;
    }

    /*
    获取套餐信息
     */
    public JSONObject packAgeInfo(String packageId) throws CarsmartException {
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isBlank(packageId)) {
            return jsonObject;
        }
        BaseExample example = this.simFlowPackageInfoManager.getExample();
        example.or().andPropertyEqualTo(packageId, "flowPackageId");
        List<SimFlowPackageInfo> sfpis = this.simFlowPackageInfoManager.listByBaseExample(example);
        if (sfpis != null && sfpis.size() > 0) {
            SimFlowPackageInfo simFlowPackageInfo = sfpis.get(0);
            jsonObject.put("packageId", simFlowPackageInfo.getFlowPackageId());//套餐编号
            jsonObject.put("name", simFlowPackageInfo.getName());//套餐名称

            String operator = "未知";
            if (simFlowPackageInfo.getOperator() != null) {
                switch (simFlowPackageInfo.getOperator()) {
                    case 1:
                        operator = "移动";
                        break;
                    case 2:
                        operator = "电信";
                        break;
                    case 3:
                        operator = "联通";
                        break;
                }
            }
            jsonObject.put("operator", operator);//运营商
            jsonObject.put("flowTotal", simFlowPackageInfo.getFlowTotal());//套餐内总流量
            jsonObject.put("activeMonth", simFlowPackageInfo.getActiveMonth());//套餐生效月数
            jsonObject.put("type", simFlowPackageInfo.getType());//套餐类型
            jsonObject.put("price", simFlowPackageInfo.getPrice());//套餐价格

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            jsonObject.put("created", "");//创建时间
            if(simFlowPackageInfo.getCreated() != null){
                jsonObject.put("created", sdf.format(simFlowPackageInfo.getCreated()));//创建时间
            }
            jsonObject.put("updated", "");//更新时间
            if(simFlowPackageInfo.getModified() != null){
                jsonObject.put("updated", sdf.format(simFlowPackageInfo.getModified()));//更新时间
            }
        }

        return jsonObject;
    }
}
